package com.alexis.portknocking;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class HostList extends ListActivity {	
	private static final int ACTIVITY_ADD = 0;
	private static final int ACTIVITY_EDIT = 0;
	
	private int activity_mode = DBAdapter.START_MODE;
	
	private DBAdapter dbadapter;
	private MenuItem upmenu;
	private MenuItem downmenu;
	private int length = 0;
	
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle("Port knocking settings");
        
    	Bundle extras = getIntent().getExtras();
    	if (extras != null) {
    		activity_mode = extras.getInt(DBAdapter.KEY_STARTSTOP);
    	}
        
        dbadapter = new DBAdapter(this.getApplicationContext());
    	dbadapter.open();
    	
    	updateList();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem menuit = menu.add(0, Menu.FIRST, 0, "Add");
        menuit.setIcon(android.R.drawable.ic_menu_add);
    	
        upmenu = menu.add(0, Menu.FIRST+1, 0, "Up");
        upmenu.setIcon(R.drawable.up);
        
        downmenu = menu.add(0, Menu.FIRST+2, 0, "Down");
        downmenu.setIcon(R.drawable.down);

        return true;
    }
    
    public boolean onPrepareOptionsMenu(Menu menu) {
    	if (getSelectedItemId() >= 0) {
    		upmenu.setEnabled(true);
    		downmenu.setEnabled(true);
    	} else {
    		upmenu.setEnabled(false);
    		downmenu.setEnabled(false);
    	}
    	return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case Menu.FIRST:
            Intent i = new Intent(this, HostEdit.class);
            i.putExtra(DBAdapter.KEY_STARTSTOP, activity_mode);
            startActivityForResult(i, ACTIVITY_ADD);
            return true;
        case Menu.FIRST+1:
        	if (getSelectedItemPosition() > 0) {
        		dbadapter.upHost(getSelectedItemId());
        		updateList();
        	}
        	return true;
        case Menu.FIRST+2:
        	if (getSelectedItemPosition() < length-1) {
        		dbadapter.downHost(getSelectedItemId());
            	updateList();
        	}
        	return true;
        }
        
        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, HostEdit.class);
        i.putExtra(DBAdapter.KEY_ID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }
    
    public void updateList() {
    	Cursor hostsCursor = dbadapter.getRawHosts(activity_mode);
    	startManagingCursor(hostsCursor);
    	
    	String[] from = new String[]{"host", "port"};
    	int[] to = new int[]{R.id.TextView01, R.id.TextView02};
    	
    	SimpleCursorAdapter hosts = new SimpleCursorAdapter(this, R.layout.hostitem, hostsCursor, from, to);
    	setListAdapter(hosts);
    	length = hosts.getCount();
    }
}
